// packageA/pages/consumableLogistics/consumableLogistics.js
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
//去掉末尾的0
const regexp=/(?:\.0*|(\.\d+?)0+)$/;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //列表
    list:[],
    //无数据
    showNothing:false,
    //折叠面板
    activePart: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);

      console.log('维修工单-物流-传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      //获取列表数据
      this.getList();
    }
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: false,
      className: 'afterSaleConsumableOrder',
      data: {
        id: this.data.paramObj.repairId,
        selectFields: ['id','logistics']
      }
    }

    CommonService.goFindList(param).then(res => {
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0];
        if(result.logistics && result.logistics.length){
          console.log('物流列表');
          console.log(result.logistics); 

          //物流费用保留两位小数并去掉末尾的0
          result.logistics.forEach(item => {
            if(item.freight!=null && item.freight!=''){
              item.freight=(Number(item.freight)).toFixed(2).replace(regexp,'$1');
            }

            //配件费
            if(item.parts && item.parts.length){
              item.parts.forEach(item2 => {
                if(item2.salePrice!=null && item2.salePrice!=''){
                  item2.salePrice=(Number(item2.salePrice)).toFixed(2).replace(regexp,'$1');
                }
              });
            }

            //省市区+地址
            let str='';
            if(item.addressArea && item.addressArea.length){
              item.addressArea.forEach(item2=>{
                str+=item2.name+'-';
              });
            }
            item.self_address=str+item.shippingAddress;            
          });
  
          this.setData({
            list: result.logistics
          })
        }else{
          this.setData({
            showNothing: true
          })
        }
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //添加/编辑
  goToAdd(e){
    let obj={
      repairId: this.data.paramObj.repairId
    }

    if(e.currentTarget.dataset.detail){
      let detail=e.currentTarget.dataset.detail;
      obj.logisticsId=detail.id;
    }  

    wx.navigateTo({
      url: "/packageA/pages/consumableLogisticsAdd/consumableLogisticsAdd?paramObj="+JSON.stringify(obj)
    })
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      showNothing: false
    })
    this.getList();
  },

  //列表页并刷新数据
  refreshListAndPage2(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.refreshListAndPage();
  },

  //删除
  onDelete(e){
    Dialog.confirm({
      title: '确定删除物流信息吗',
      confirmButtonText:'确定',
      cancelButtonText:'不删除了'
    }).then(() => {
      let detail=e.currentTarget.dataset.detail;
      let list=this.data.list;
      let arr=[];

      list.forEach(item => {
        if(item.id != detail.id){
          arr.push(item);
        }	
      });

      let param={
        className:'afterSaleConsumableOrder',
        data:{
          id: this.data.paramObj.repairId,
          logistics: arr
        }
      }

      CommonService.goSave(param).then(res=>{        
        if (res.statusCode == 200 && res.data.success == true){
          Toast.success('已删除');
          setTimeout(() => {
            //刷新列表和页码
            this.refreshListAndPage();
            this.refreshListAndPage2();
          }, 1000);
        }else{
          Toast.fail('操作失败');
        }
      }).catch((error) => {
        Toast.fail('操作失败');
      })

    }).catch(() => {
    });
  },

  //切换配件折叠面板
  onChangePart(event) {
    this.setData({
      activePart: event.detail,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})